﻿using Common;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using IP20.Procdure;
using IP20.View;

namespace IP20.ViewModel
{
    internal class UserManageViewModel : NotifyBase
    {
        private string _userErr;

        public string UserErr
        {
            get { return _userErr; }
            set
            {
                _userErr = value;
                this.DoNotify();
                if (value != null)
                {
                    Task.Run(() => { Thread.Sleep(1000); UserErr = ""; });
                }
            }
        }

        public ObservableCollection<UserEntity> Users { get; set; }
        public CommandBase AddUserCommand { get; set; }
        public CommandBase DelUserCommand { get; set; }
        public CommandBase EditUserCommand { get; set; }

        public UserEntity SelectedUser { get; set; }

        public UserManageViewModel()
        {
            AddUserCommand = new CommandBase(AddUser);
            DelUserCommand = new CommandBase(DelUser);
            EditUserCommand = new CommandBase(EditUser);

            Users = new ObservableCollection<UserEntity>();
            foreach (var item in UserHelper.GetInstance().UserList)
            {
                Users.Add(item);
            }
        }

        private void EditUser(object obj)
        {
            try
            {
                UserEditView userManagerView = new UserEditView(SelectedUser);
                if (userManagerView.ShowDialog() == true)
                {
                    var user = Global.EditUser;
                    if (Users.Where(x => x.UserName == user.UserName).Count() == 0)
                    {
                        UserErr = "用户不存在";
                        return;
                    }

                    UserHelper.GetInstance().EditUser(user);
                    Users.Clear();
                    foreach (var item in UserHelper.GetInstance().UserList)
                    {
                        Users.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                UserErr = ex.Message;
            }
        }

        private void DelUser(object obj)
        {
            try
            {
                if (SelectedUser is null)
                {
                    UserErr = "请选择一个用户";
                    return;
                }
                UserHelper.GetInstance().DeleteUser(SelectedUser.UserName);
                Users.Clear();
                foreach (var item in UserHelper.GetInstance().UserList)
                {
                    Users.Add(item);
                }
            }
            catch (Exception ex)
            {
                UserErr = ex.Message;
            }
        }

        private void AddUser(object obj)
        {
            try
            {
                UserEditView userManagerView = new UserEditView(null);
                if (userManagerView.ShowDialog() == true)
                {
                    var user = Global.EditUser;
                    if (Users.Where(x => x.UserName == user.UserName).Count() > 0)
                    {
                        UserErr = "用户已存在";
                        return;
                    }

                    UserHelper.GetInstance().AddUser(user);
                    Users.Clear();
                    foreach (var item in UserHelper.GetInstance().UserList)
                    {
                        Users.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                UserErr = ex.Message;
            }
        }
    }
}